$(document).on("pageInit","#page-shop-shop_shopping_cart", function(e, pageId, $page) {

	var shopping_cart_top = $("input[name=shopping-cart-top]"),
		shopping_cart = $("input[name=shopping-cart]"),
		shopping_cart_all = $("input[name=shopping-cart-all]");
	$(shopping_cart_top).click(function(){
		var self = $(this), 
		id = self.attr("data-id");
		if(shopping_cart_top.is(":checked")){
			$(".input-check-"+id).prop('checked',true);
			
		}else{
			$(".input-check-"+id).prop('checked',false);
			$(shopping_cart_all).prop('checked',false);
		}
		var sum = 0;
		$("input[name=shopping-cart]:checked").each(function(){
			sum += parseInt($(this).parent().find(".input-money").val());
			
		});
		$(".J-money").html(sum);
		var card_length = $(this).parents(".card").length;
		var checked_top_length = $(this).parents(".card").find("input[name=shopping-cart-top]:checked").length;
		if (checked_top_length == card_length) {
			shopping_cart_all.prop('checked',true);
		}
		console.log(sum);
	});
	$(shopping_cart).click(function(){
		if(shopping_cart.is(":checked")){
			
		}else{
			$(shopping_cart_top).prop('checked',false);
			$(shopping_cart_all).prop('checked',false);
		}
		var card_content_length = $(this).parents(".card").find(".card-content").length;
		var checked_length = $(this).parents(".card").find("input[name=shopping-cart]:checked").length;
		
		if (checked_length == card_content_length) {
			$(this).parents(".card").find("input[name=shopping-cart-top]").prop('checked',true);
		}
		var card_length = $(this).parents(".card").length;
		var checked_top_length = $(this).parents(".card").find("input[name=shopping-cart-top]:checked").length;
		if (checked_top_length == card_length) {
			shopping_cart_all.prop('checked',true);
		}
	});

	//编辑
	$(".J-edit").click(function(){
		var self = $(this);
		var txt = self.html();
		var goods_id = self.attr("data-id"), podcast_id = self.attr("data-podcast_id"), number = self.parents(".card").find("input[name=amount]").val();
		if (txt =="编辑") {
			self.html('完成');
		}else if (txt =="完成") {
			self.html('编辑');
		    handleAjax.handle(TMPL + "index.php?ctl=shop&act=update_shopping_goods", { goods_id: goods_id, podcast_id:podcast_id, number:number}).done(function(resp) {
	            setTimeout(function() {
	                window.location.reload();
	            }, 1000);
	        }).fail(function(err) {
	            $.toast(err, 1000);
	        });
		}
		$(".goods-text").toggleClass("active");
		$(".goods-edit").toggleClass("active");
	});

	//删除
	$(".J-delete").click(function(){
		var self = $(this);
		var parents_card = self.parents(".card");
		
		var goods_id = self.attr("data-id"), podcast_id = self.attr("data-podcast_id"), number = self.parents(".card").find("input[name=amount]").val();
		handleAjax.handle(TMPL + "index.php?ctl=shop&act=delete_shopping_goods", { goods_id: goods_id, podcast_id:podcast_id, number:number}).done(function(resp) {
            setTimeout(function() {
                parents_card.remove();
            }, 1000);
        }).fail(function(err) {
            $.toast(err, 1000);
        });
	});

	$(shopping_cart_all).click(function(){
		if(shopping_cart_all.is(":checked")){
			$(".input-check").prop('checked',true);
		}else{
			$(".input-check").prop('checked',false);
		}
		var sum = 0;
		$("input[name=shopping-cart]:checked").each(function(){
			sum += parseInt($(this).parent().find(".input-money").val());
			
		});
		$(".J-money").html(sum);
		console.log(sum);
	});
	shopping_cart.click(function(){
		var sum = 0;
		$("input[name=shopping-cart]:checked").each(function(){
			sum += parseInt($(this).parent().find(".input-money").val());
			
		});
		$(".J-money").html(sum);
		console.log(sum);
	});
	$(".J-settlement").click(function(){
		// var input_checked = shopping_cart.prop("checked");
		// if (input_checked) {
		// 	var money_checked = $(".input-check").is(":checked").parent().find(".input-money").val();
		// }
		
	});
	//增加购买数量
    $(".input-goods-num").val(0);
    $(document).on('click', '.add', function() {
        var self = $(this);
        var goods_id = self.attr("data-id");
        var num = parseInt($(this).siblings(".input-goods-num").val()) || 0;
        if(num < 99){
           num = num + 1;
        }
        $(this).siblings(".input-goods-num").val(num);
    });
    //减少购买数量
    $(document).on('click', '.lost', function() {
        var self = $(this);
        var goods_id = self.attr("data-id");
        var num = parseInt($(this).siblings(".input-goods-num").val()) || 0;
        if(num > 0){
            num = num - 1;
        }
        $(this).siblings(".input-goods-num").val(num);
    });
    $('.input-goods-num').blur(function() {
        var self = $(this);
        var goods_id = self.attr("data-id");
    });
    $('.input-goods-num').bind('input propertychange', function() {
        var self = $(this),
            self_num = self.val();
        // if (self_num) {
        //     $(".input-goods-num").not(self).val(0);
        // }
        if (self_num>99) {
            $(this).val(99);
        }
    });

    //结算
    $(document).on('click', '.J-settlement', function(){
    	var shop_arr = [];
	    $(".goods-item").each(function(){
	        var self = $(this), input_amount = self.find("input[name='input-number']");
	        if(input_amount.val()>0){
	            shop_arr.push({podcast_id:data.podcast_id,goods_id:input_amount.attr("data-id"),number:input_amount.val()});  
	        }
	    });
	    var data_shop_arr =  JSON.stringify(shop_arr);
	    handleAjax.handle(APP_ROOT + "/wap/index.php?ctl=shop&act=goods_inventory", {shop_info:data_shop_arr}, '', 1).done(function(result) {
	        if (shop_arr.length) {
	            if (result.status == 1) {
	                location.href = TMPL + "index.php?ctl=shop&act=order_settlement_user&shop_info="+data_shop_arr;
	            } else {
	                $.toast(result.error);
	            }
	        } else {
	            $.toast("请先选择商品");
	            return false;
	        }

	    }).fail(function(err) {
	        $.toast(err);
	    });
    });
});